package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.util.base.BaseAction;

import com.sun.istack.logging.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;


@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class WayBillAction extends BaseAction<WayBill> {
   private static final Logger LOGGER =   Logger.getLogger(WayBillAction.class);
    @Autowired
    private WayBillService wayBillService;
    @Action(value ="waybill_save",results = {
            @Result(name = "success",type = "json")
    })
    public String save(){
        Map<String,Object> result = new HashMap<>();
        if (model.getOrder()!=null &&(model.getOrder().getId() ==null ||model.getOrder().getId()==0)){
            model.setOrder(null);
        }
        try {
            wayBillService.save(model);
            result.put("success",true);
            result.put("msg","保存运单成功");
            LOGGER.info("运单保存成功!!!!!!");
        }catch (Exception e) {
            result.put("success",false);
            result.put("msg","保存运单失败");
            LOGGER.info("运单保存失败!!!!!!");
            e.printStackTrace();
        }
        ServletActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }
    @Action(value = "pageQuery",results = {
            @Result(name = "success",type = "json")
    })
    public  String pageQuery (){
        Pageable pageable = new PageRequest(page-1,rows);
        Page<WayBill> pageData = wayBillService.pageQuery(pageable);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageData.getTotalElements());
        map.put("rows",pageData.getContent());
        ServletActionContext.getContext().getValueStack().push(map);

        return SUCCESS;
    }

    @Action(value = "wayBill_findByWayBillNum",results = {
            @Result(name = "success",type = "json")
    })
    public  String findByWayBillNum(){
        WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
        Map<String ,Object> result = new HashMap<>();

        if (wayBill== null) {
            result.put("success",false);
        }else {

            result.put("success",true);
            result.put("wayBillData",wayBill);
            System.out.println(wayBill);
        }
        ServletActionContext.getContext().getValueStack().push(result);
        return SUCCESS;
    }

}
